/*
 * Copyright (C)  Tony Green, Litepal Framework Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wifibrowser.lib;


import org.apache.cordova.LOG;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.KeyEvent;


/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年3月10日 上午10:12:47
 * @Title <pre>Base Application</pre>
 * @Description <pre>todo</pre>
 */
public class BaseApplication extends Application {

	/**
	 * Global application context.
	 */
	protected /*static*/ Context mContext;
	 
	protected static BaseApplication instance;

	/**
	 * Construct of LitePalApplication. Initialize application context.
	 */
	public BaseApplication() {
		mContext = this;
		instance=this;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		instance=this;
		mContext = this;
		//发布
		//Log.setLogLevel(Log.LEVEL_RELEASE);
		//LOG.setLogLevel(LOG.LOGLEVEL);
		Log.setLogLevel(Log.LEVEL_VERBOSE);
		LogCat.deleteLogFile();
		LOG.setLogLevel(LOG.VERBOSE);
		//if(!Log.isDebug())
		AppUncaughExceptionHandler.getInstance().register(this);
	}

	/**
	 *  get application instance
	 * @return
	 */
	public synchronized static BaseApplication getInstance(){
		return instance;
	}
	
/*	private static HashMap<String, Application> instanceMap = new HashMap<String, Application>();
	protected static Application instance(String instanceName)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		if (instanceMap.get(instanceName) == null
				|| !(instanceMap.get(instanceName) instanceof BaseApplication)) {
			instanceMap.put(instanceName,
					(Application) Class.forName(instanceName).newInstance());
		}
		return instanceMap.get(instanceName);
	}*/
	
	/**
	 * Get the global application context.
	 * @return Application context.
	 * @throws AppException
	 */
	public /*static*/ Context getContext() {
		if (mContext == null) {
			//throw new AppException(AppException.APPLICATION_CONTEXT_IS_NULL);
			mContext=getApplicationContext();
		}
		return mContext;
	}
	
	
	@Override
	public void onLowMemory() {
		super.onLowMemory();
		mContext = getApplicationContext();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Application#onTrimMemory(int)
	 */
	@SuppressLint("NewApi")
	@Override
	public void onTrimMemory(int level) {
		super.onTrimMemory(level);
	}
	
	/**
	 * @param context
	 * @return String[0]=versionName;String[0]=versionCode;
	 */
	public String[] getAppInfor() {
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo=null;
		String []appinfo=new String[2];
		try {
			// 获取packagemanager的实例
			PackageManager packageManager = getPackageManager();
			packInfo = packageManager.getPackageInfo(getPackageName(), 0);
			String versionName = packInfo.versionName;
			int versionCode=packInfo.versionCode;
			appinfo[0]=versionName;
			appinfo[1]=String.valueOf(versionCode);
		} catch (Exception e) {
			Log.e(e.getMessage());
		}
		return appinfo;
	}
	
	
	long time = 0l;

	/**
	 * @Description <pre>退出应用</pre>
	 * @param keyCode 按键ID
	 * @return boolean
	 */
	public boolean exitApp(final Activity mAct, int keyCode) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - time) > 3000) {
				ToastCompat.showToastLong(mContext, "再按一次退出应用");
				time = System.currentTimeMillis();
			} else {
				mAct.finish();
				int pid = android.os.Process.myPid();
				android.os.Process.killProcess(pid);
				System.exit(-1);
			}
			return true;
		}
		return false;
	}

}
